package com.c.ctgproject.ui.other

import android.app.ProgressDialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.CTGproject
import com.c.ctgproject.mode.StorageVehicles
import com.c.ctgproject.mode.VehicleInspection
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.ui.files.MainViewModel
import com.c.ctgproject.ui.newvehicles.Newvehicles
import com.c.ctgproject.ui.vehicleinspection.VehicleInspectionActivity
import com.c.ctgproject.ui.vehicleinspection.VehicleInspectiontActivity
import com.c.ctgproject.utils.*
import com.jaeger.library.StatusBarUtil
import kotlinx.android.synthetic.main.activity_inputplate.*
import kotlinx.android.synthetic.main.title.*

/**
 * 手动输入 页面
 */
class InputPlateActivity : AppCompatActivity(), View.OnClickListener
//    , View.OnTouchListener
{
    private val statusColor: StatusColor = StatusColor()
//    private var keyboardUtil: CarKeyboardUtil? = null
    private var token: String? = null
    private var mainViewModel: MainViewModel? = null
    private var progressDialog: ProgressDialog? = null
    private var dialogUtils: DialogUtils? = null
    private var builder: AlertDialog.Builder? = null
    private var menuName: String? = null//所属角色

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_inputplate)
        StatusBarUtil.setTranslucent(this)
        statusColor.setAndroidNativeLightStatusBar(this, false)
        toolbar_title.text = "手动输入"
        setSupportActionBar(toolbar)
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        token = Utils.getShared2(this, "token")
        event()
        //角色
        menuName= CTGproject.getBaseApplication().characterName
    }

    private fun event() {
        progressDialog = ProgressDialog(
            this@InputPlateActivity,
            R.style.AppTheme_Dark_Dialog
        )
        progressDialog!!.isIndeterminate = true
        progressDialog!!.setCanceledOnTouchOutside(false)
        progressDialog!!.setCancelable(true)
        mainViewModel = ViewModelProvider(
            this, ViewModelProvider.AndroidViewModelFactory(application)
        ).get(MainViewModel::class.java)
        //车辆入场回调监听
        mainViewModel!!.getInitialInspectionCardata().observe(this, Observer { respone: Response<VehicleInspection> ->
            if (respone.status == 0) {
                progressDialog!!.dismiss()
                MyLog.d("都有什么:${respone.data}")
                //进入车辆初检
                val intent = Intent(applicationContext, VehicleInspectionActivity::class.java)
                intent.putExtra("carOwner", respone.data.carOwner)
                intent.putExtra("ownerPhone", respone.data.ownerPhone)
                intent.putExtra("ownerPlate", respone.data.ownerPlate)
                intent.putExtra("carId", respone.data.carId)
                intent.putExtra("salesman", respone.data.salesman)
                startActivity(intent)
            } else {
                progressDialog!!.dismiss()
                builder = AlertDialog.Builder(this@InputPlateActivity).setTitle("提示！")
                    .setMessage(respone.msg).setPositiveButton("新建车辆") { _, _ ->
                        val intent = Intent(applicationContext, Newvehicles::class.java)
                        intent.putExtra("carPlate", etPlateNumber.text.handlerNull())
                        startActivity(intent)
                    }.setNegativeButton("取消") { dialogInterface, _ ->
                        dialogInterface.dismiss()
                    }
                builder!!.create().show()
            }
        })

        mainViewModel!!.getStorageVehiclesdata().observe(this, Observer { response: Response<StorageVehicles> ->
            if (response.status == 1) {
                progressDialog!!.dismiss()
                Toast.makeText(this, response.msg, Toast.LENGTH_LONG).show()
            } else {
                progressDialog!!.dismiss()
                val intent = Intent(applicationContext, VehicleInspectiontActivity::class.java)
                intent.putExtra("carPlate", etPlateNumber.text.handlerNull())
                startActivity(intent)
            }
        })

        confirm_button.setOnClickListener(this)
//        keyboardUtil = CarKeyboardUtil(this, etPlateNumber)
//        etPlateNumber.setOnTouchListener(this)
//        etPlateNumber.addTextChangedListener(object : TextWatcher {
//            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
//
//            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
//                val text = s.handlerNull()
//                if (s.length == 8) {
//                    rlPlateNumber.setBackgroundResource(R.drawable.btn_round_blue)
//                    imgIcon.visibility = View.VISIBLE
//                } else {
//                    rlPlateNumber.setBackgroundResource(R.drawable.btn_round_green)
//                    imgIcon.visibility = View.GONE
//                }
//                if (text.contains("港") || text.contains("澳") || text.contains("学")) {
//                    etPlateNumber.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(7))
//                } else {
//                    etPlateNumber.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(8))
//                }
//            }
//
//            override fun afterTextChanged(s: Editable) {}
//        })


    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {
        //识别按钮
        if (v == confirm_button) {
            if (TextUtils.isEmpty(etPlateNumber.text.handlerNull())) {
                Toast.makeText(this, "车牌号不能为空", Toast.LENGTH_LONG).show()
            } else if (etPlateNumber.text.length < 7) {
                Toast.makeText(this, "车牌号格式不对", Toast.LENGTH_LONG).show()
            } else {
                progressDialog!!.setMessage("加载中...")
                progressDialog!!.show()
                if (menuName.equals("仓储部")) {
                    //仓储部
                    mainViewModel!!.primaryInspection(token!!, etPlateNumber.text.handlerNull())
                } else {
                    //档案部
                    mainViewModel!!.getInitialInspectionCar(token!!, etPlateNumber.text.handlerNull())
                }
            }


        }
    }


//    @SuppressLint("ClickableViewAccessibility")
//    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
//        if (v!!.getId() == R.id.etPlateNumber) {
//            keyboardUtil!!.hideSystemKeyBroad()
//            keyboardUtil!!.hideSoftInputMethod()
//            if (!keyboardUtil!!.isShow) keyboardUtil!!.showKeyboard()
//            confirm_button.visibility = View.INVISIBLE
//        } else {
//            if (keyboardUtil!!.isShow) keyboardUtil!!.hideKeyboard()
//            confirm_button.visibility = View.VISIBLE
//        }
//        return false
//    }
//
//    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        if (keyboardUtil!!.isShow) {
//            keyboardUtil!!.hideKeyboard()
//            confirm_button.visibility = View.VISIBLE
//        }
//        return super.onTouchEvent(event)
//    }
}